package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;

public class AccountManagerServlet extends ImageServlet {
    private EJBDatabaseQuery ejbdb;
    public void doPost(HttpServletRequest request,  HttpServletResponse response)
    {
	try {
	    response.setContentType("text/html");
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();

	    String  user_ip      = request.getRemoteAddr();

	    String call_path     = request.getContextPath()+request.getServletPath();

	    String mid        = (String)request.getAttribute("mid");
	    String sub_dir    = request.getParameter("sub_dir");
	    String table_name = request.getParameter("table_name");

	    if(mid == null || sub_dir==null || table_name == null) {
		return;
	    }

	    //================================================================
	    // System propperties
	    //================================================================

	    ResourceBundle SystemInfo = (ResourceBundle)session.getAttribute("SystemInfo");
	    if (SystemInfo == null) {
		return;
	    }

	    String post_home = SystemInfo.getString("3wcenter.default.post.home");

	    //=================================================================
	    // sub-directory info
	    //=================================================================

	    Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	    if(dir_hs == null) {
		return;
	    }

	    //=================================================================
	    // user info
	    //=================================================================

	    Hashtable user_info  =  (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		return;
	    }

	    String user_name  = (String)user_info.get("user_name");
	    int user_age      = Integer.parseInt((String)user_info.get("user_age"));
	    int user_level    = Integer.parseInt((String)user_info.get("user_level"));
	    String user_fname = (String)user_info.get("user_fname");
	    String user_since = (String)user_info.get("user_since");
	    String user_edu   = (String)user_info.get("user_edu");
	    String user_zip   = (String)user_info.get("user_zip");
	    String user_sex   = (String)user_info.get("user_sex");

	    if(user_name == null || user_name.equals("guest")) {
		return;
	    }

	    String action =  request.getParameter("aid");	  

	    AccountManager am = new AccountManager();

	    StringBuffer sbf = am.getManager(sub_dir, user_name);
	    sbf.append(am.getLinkBar(call_path, mid, "1-0", "1-1", "1-2"));

	    //==================================================================
	    // view  profile
	    //==================================================================

	    if(action == null || action.equals("1-0")) {
		sbf.append(am.getUserProfile(user_name, user_level));
		out.println(sbf);
	    }

	    //==================================================================
	    // Change password
	    //==================================================================

	    if(action != null && action.equals("1-1")) {
		sbf.append(am.changePassword(request, user_name));
		out.println(sbf);
	    }

	    //==================================================================
	    // Update user profile
	    //==================================================================

	    if(action != null && action.equals("1-2")) {
		sbf.append(am.updateUserProfile(request, mid, session.getId(), user_name));
		out.println(sbf);
	    }
	    

	    ///================================================================
	    // User preferrences
	    //=================================================================

	    if(sub_dir.equals("Account/Preferences")){
	    }
	}
	catch(IOException e) {
	    System.out.println("Error:AccountManagerServlet:["+e+"]");
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }
}






